<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/5/3/003
 * Time: 16:10
 */

namespace app\api\model;

use EasyWeChat\Staff\Session;
use think\Model;
use think\Cookie;
use think\Cache;
use think\Session as Sess;
use think\Db;

class StoreComment extends Model
{
    // 开启自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'createtime';
    //缓存对象


    //插入数据
    public function insertComment($arr)
    {
        //新增评论，修改平均分
        $data = $this->data($arr)->allowField(true)->save();
        $score = $this->where(['store_id' => $arr['store_id']])->avg('score');
        $score = round($score,2);
        Db::table('uct_hk_store')->where(['id' => $arr['store_id']])->update(['s_score' => $score]);
        if($data == 1){//新增成功
            //改变consume
            Db::table('uct_consume_card')->where('id',$arr['consume_id'])->update(['is_comment' => 1]);
            return ['_code' => '200','msg' => '评论成功'];
        }else{//失败
            return ['_code' => '400','msg' => '评论失败'];
        }
    }

    //查看评论
    public static function getComment($consume_id)
    {
//        $data['data'] = self::where(['consume_id'=>$consume_id])->find();
        $field = ['comment.score','comment.comment','card.logo_url','card.title','comment.createtime'];
        $data =  Db::table('uct_store_comment')->alias('comment')
                 ->join('uct_consume_card consume','consume.id = comment.consume_id')
                 ->join('uct_wechat_card card','card.card_id = consume.card_id')
                 ->where(['comment.consume_id' => $consume_id])
                 ->field($field)
                 ->find();
        $data['time'] = date('m-d',$data['createtime']);
        return $data;
    }

    //获取店铺下的评论列表
    public static function getStoreComment($store_id,$user_id)
    {
        $page = input('page',1);
        $row = input('row',5);
        $field = ['comment.comment','user.nickname','user.avatar','comment.like_num','comment.createtime','comment.id'];

        $data['data'] = Db::table('uct_store_comment')->alias('comment')
            ->join('uct_user user','comment.user_id = user.id')
//            ->join('uct_hk_store store','store.id = '.$store_id)
            ->where(['comment.store_id' => $store_id])
            ->order('like_num desc,comment.createtime desc')
            ->field($field)
            ->page($page,$row)
            ->select();

        $data['total'] = $total = Db::table('uct_store_comment')->alias('comment')
            ->join('uct_user user','comment.user_id = user.id')
            ->where(['comment.store_id' => $store_id])
            ->count();

        foreach($data['data'] as $key => &$val){
            $val['time'] = date("Y-m-d",$val['createtime']);
            $options = [
                // 缓存类型为File
                'type'  =>  'File',
                // 缓存有效期为永久有效
                'expire'=>  0,
                // 指定缓存目录
                'path'  =>  APP_PATH.'runtime/cache/user_comment'
            ];
            Cache::connect($options);
            $comment_list = Cache::get('list_'.$user_id);
            if($comment_list && in_array($val['id'],$comment_list)){//已点赞
                $val['is_like'] = 1;
            }else{//未点赞
                $val['is_like'] = 0;
            }
        }

        $data['has_more'] = ceil($total / $row) > $page ? true : false;

        return $data;
    }

    //点赞
    public static function likeComment($user_id,$comment_id)
    {
        $options = [
            // 缓存类型为File
            'type'  =>  'File',
            // 缓存有效期为永久有效
            'expire'=>  0,
            // 指定缓存目录
            'path'  =>  APP_PATH.'runtime/cache/user_comment'
        ];
        Cache::connect($options);
        $comment_list = Cache::get('list_'.$user_id);
        if($comment_list && in_array($comment_id,$comment_list)){//判断评论id是否在评论列表中
            self::where(['id' => $comment_id])->setDec('like_num',1);
            $key = array_search($comment_id,$comment_list);
            array_splice($comment_list,$key,1);
            Cache::set('list_'.$user_id,$comment_list,315360000);
            return ['_code' => '200', 'msg' => '取消点赞'];
        }else {
            $data = self::where(['id' => $comment_id])->setInc('like_num');
            if ($data == 1) {//点赞成功
                $list = Cache::get('list_'.$user_id);
                if($list){//存在的
                    $list[] = $comment_id;
                    Cache::set('list_'.$user_id,$list,315360000);
                }else{//首次
                    Cache::set('list_'.$user_id,[$user_id],315360000);
                }
                return ['_code' => '200', 'msg' => '点赞成功'];
            } else {
                return ['_code' => '400', 'msg' => '点赞失败'];
            }
        }
    }
}